//
//  YsDocUploadModel.h
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/2/24.
//

#import "YsDocUploadModel.h"
#import "YsUploadManager.h"
#import "YsCloudFileManagerHeader.h"
#import <YYKit/YYKit.h>
#import "YsCloudFileRealmManager.h"
#import "YsFileUtil.h"


@implementation YsDocUploadModel

@synthesize filePath = _filePath;

- (instancetype)initWithProductUploadModel:(BOOL)productLoadModel
{
    self = [super init];
    if (self) {
        if (productLoadModel) {
            self.fileUploadReamModel = [[YsCloudFileUploadReamModel alloc] init];
            self.fileUploadReamModel.uploadID = [self.fileUploadReamModel incrementaID];
            self.fileUploadReamModel.accountCode = kAccountCode;
            self.fileUploadReamModel.createTime = [NSString currentTimeStr];
        }
    }
    return self;
}

- (void)setUploadStatus:(int )uploadStatus
{
    _uploadStatus = uploadStatus;
    if (uploadStatus == 2) {
        self.waitTaskTime = NSString.currentMillisecondTime;
    }
    if (uploadStatus && self.fileUploadReamModel) {
    __unused  BOOL success =  [YsCloudFileRealmManager updateObj:^{
             self.fileUploadReamModel.uploadStatus = uploadStatus;
         }];
    }
}

- (void)setUploadedCount:(NSInteger)uploadedCount {
    
    _uploadedCount = uploadedCount;
    if (self.fileUploadReamModel.uploadedCount !=0 && self.fileUploadReamModel) {
   __unused  BOOL success =  [YsCloudFileRealmManager updateObj:^{
            self.fileUploadReamModel.uploadedCount = (int)uploadedCount;
        }];
    }else{
        if (self.fileUploadReamModel) {
            [YsCloudFileRealmManager updateObj:^{
                self.fileUploadReamModel.uploadedCount = (int)uploadedCount;
            }];
        }
    }
    self.uploadPercent = (CGFloat)uploadedCount / (self.totalCount -1);
    self.progressLableText = [NSString stringWithFormat:@"%.2fMB/%.2fMB",self.totalSize * self.uploadPercent /1024.0/1024.0,self.totalSize/1024.0/1024.0];
    if (self.progressBlock) {
        self.progressBlock(self.uploadPercent,self.progressLableText);
    }
    
    [[YsUploadManager shareYsUploadManager] refreshCaches];
}


- (void)setUploadPercent:(CGFloat)uploadPercent
{
    _uploadPercent = uploadPercent;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
           self.fileUploadReamModel.uploadPercent = uploadPercent;
       }];
    }
}

#pragma mark - 一下都是为了给realm模型赋值
- (void)setFileHash:(NSString *)fileHash
{
    _fileHash = fileHash;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
           self.fileUploadReamModel.md5 = fileHash;
       }];
    }
}

- (void)setFilePath:(NSString *)filePath
{
    if ([filePath containsString:YsFileUtil.getHomePath]) {
        _filePath  = filePath;
    }else{
        /// 先把后面相同的截取出来
      NSString *samePath = [filePath substringFromIndex:YsFileUtil.getHomePath.length];
      _filePath = [NSString stringWithFormat:@"%@%@",YsFileUtil.getHomePath,samePath];
    }
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.fileUploadReamModel.filePath = filePath;
        }];
    }
}

- (NSString *)filePath
{
    if ([_filePath containsString:YsFileUtil.getHomePath]) {
        return _filePath;
    }else{
        /// 先把后面相同的jie
        NSString *samePath = [_filePath substringFromIndex:YsFileUtil.getHomePath.length];
        return [NSString stringWithFormat:@"%@%@",YsFileUtil.getHomePath,samePath];
    }
}

- (void)setLastPathComponent:(NSString *)lastPathComponent
{
    _lastPathComponent = lastPathComponent;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
             self.fileUploadReamModel.mime = lastPathComponent;
         }];
    }
}

- (void)setTotalSize:(int64_t)totalSize
{
    _totalSize = totalSize;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
           self.fileUploadReamModel.totalSize = (int)totalSize;
       }];
    }
}

- (void)setTotalCount:(NSInteger)totalCount
{
    _totalCount = totalCount;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
            self.fileUploadReamModel.totalCount = (int)totalCount;
        }];
    }
}

- (void)setFileName:(NSString *)fileName
{
    _fileName = fileName;
    if (self.fileUploadReamModel) {
        [YsCloudFileRealmManager updateObj:^{
               self.fileUploadReamModel.fileName = fileName;
        }];
    }
}

- (void)setWaitTaskTime:(NSString *)waitTaskTime
{
    _waitTaskTime =waitTaskTime;
    if (self.fileUploadReamModel) {
       [YsCloudFileRealmManager updateObj:^{
          self.fileUploadReamModel.waitTaskTime = waitTaskTime;
       }];
   }
}
@end
